<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 资讯模型
 *
 * @author yaoyunhua
 */
namespace Admin\Model;
use Think\Model;
class NewsModel  extends Model {
    //put your code here
    protected  $tableName = 'news';
    
    protected $_auto = array ( 
         array('create_time','time',1,'function'), 
         array('update_time','time',3,'function'),
        array('user_id','getUserId',3,'callback')
     );
    
    public function getUserId(){
        return session('userId');
    }
    
    //获取广告列表(按分类分)
    public function getNewsList($where=array(),$limit=10){
        $count = $this->where($where)->count();
        $p = getPage($count,$limit);
        $data = $this->where($where)->limit($p->firstRow, $p->listRows)->order('update_time desc')->select();
        return array('data'=>$data,'page'=>$p->show());

    }
    
    public function getNewsInfoById($id){
        $this->where("id = ".$id)->setInc('click'); 
        return $this->where("id = ".$id)->find();
    }
    
    public function getNewsPreById($id,$cate_id){
        $where = array(
            'id' => array('lt',$id),
            'cate_id' => array('eq',$cate_id)
        );
        return $this->where($where)->order("id desc")->find();
    }
    
    public function getNewsNextById($id,$cate_id){
        $where = array(
            'id' => array('gt',$id),
            'cate_id' => array('eq',$cate_id)
        );
        return $this->where($where)->order("id asc")->find();
    }
    
    public function delNewsById($id){
        $where = array(
            'id' => array('eq',$id)
        );
        return $this->where($where)->delete();
    }
}